import Vue from 'vue'
import VueRouter from 'vue-router'
import { LoginState } from '../utils/vui365'

// 解决报错: Unknown custom element: <router-view>
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // 后台首页
    {
      path: '/',
      name: 'Index',
      component: () => import('../views/index/Index.vue')
    },
    // 登录页
    {
      path: '/login',
      name: "Login",
      component: () => import('../views/login/Login.vue')
    },
    // 个人资料
    {
      path: '/profile',
      name: "Profile",
      meta: { requiresAuth: true },
      component: () => import('../views/login/Profile.vue')
    },
    
// 管理员管理
{
path: '/auth-admins/index',
name: "authAdminsIndex",
meta: { requiresAuth: true },
component: () => import('../views/auth-admins/Index.vue')
},

// 权限分组管理
{
path: '/auth-groups/index',
name: "authGroupsIndex",
meta: { requiresAuth: true },
component: () => import('../views/auth-groups/Index.vue')
},

// 权限规则管理
{
path: '/auth-rules/index',
name: "authRulesIndex",
meta: { requiresAuth: true },
component: () => import('../views/auth-rules/Index.vue')
},

// 系统配置管理
{
path: '/sys-configs/index',
name: "sysConfigsIndex",
meta: { requiresAuth: true },
component: () => import('../views/sys-configs/Index.vue')
},

// 系统通知管理
{
path: '/sys-notices/index',
name: "sysNoticesIndex",
meta: { requiresAuth: true },
component: () => import('../views/sys-notices/Index.vue')
},    
  ]
})

// 判断登录状态
router.beforeEach((to, from, next) => {
  let isLogin = LoginState.me().isLogin();
  console.log('[router/index.js] 判断登录状态', isLogin);
  if (!isLogin) {
    if (to.name == 'Login') {
      isLogin = true;
    } else {
      // 如需忽略登录，需要强制配置 meta: { requiresAuth: false },
      isLogin = (to.meta != undefined && to.meta.requiresAuth != undefined && to.meta.requiresAuth == false)
    }
  }
  if (!isLogin) {
    // vue3直接返回路由信息，vue2要用next跳转
    next({ name: "Login", query: { redirect: to.fullPath } });
  } else {
    next();
  }
})

export default router